
import time

from dexen.libs import algorithms, constants as dc

from libs import constants
from tasks import initialization, development, evaluation, feedback, visualization


def visualize(archive):
    obj = algorithms.getEAObject()
    obj.setObjectives([("min_width", dc.MINIMIZE), ("min_height", dc.MINIMIZE)])

    individuals = [ind for ind in archive if ind['state'] == constants.ALIVE]
    visualization.draw_pareto(individuals)
    
    obj.filterDominatedOnes(individuals)
    visualization.draw_pareto(individuals)
    """
    individuals = archive[:]
    obj.filterDominatedOnes(individuals)
    visualization.draw_pareto(individuals)
    """

def doIt():
    individuals = initialization.initialise("Hello")
    development.develop(individuals)
    evaluation.evaluate(individuals)
    
    archive = []
    archive.extend(individuals)
    new_gen = []
    cnt = len(individuals)
    while cnt < constants.MAX_INDIVIDUALS:
        combined_gen = feedback.feedback(individuals + new_gen)
        individuals = [ind for ind in combined_gen if ind['state'] == constants.ALIVE and ind.has_key('area')]
        new_gen = [ind for ind in combined_gen if not ind.has_key('area')] 
        assert len(new_gen) == constants.DEVELOPMENT_SIZE
        development.develop(new_gen)
        evaluation.evaluate(new_gen)
        archive.extend(new_gen)
        cnt += constants.DEVELOPMENT_SIZE
        #print "Total number of individuals", len(archive)
        if len(archive) % 10000 == 0:
            visualize(archive)
    